﻿using BookStore.Solution.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Solution.BLL
{
    public class IEManager
    {
        public string Insert(IE entity)
        {
            IE ie = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    ie = ctx.IEs.Where(a => a.IEID == entity.IEID).FirstOrDefault<IE>();
                    if (ie == null)
                    {
                        ctx.IEs.Add(entity);
                        if (ctx.SaveChanges() > 0)
                        {
                            SystemLogManager.Insert("Thêm chi tiêu: " + entity);
                            return "Thêm thành công";
                        }
                        else
                            return "Thêm thất bại";
                    }
                    else
                        return "Trùng khóa chính";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }
        }

        public string Update(IE entity)
        {
            IE IE = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    IE = ctx.IEs.Where(a => a.IEID == entity.IEID).FirstOrDefault<IE>();
                    if (IE != null)
                    {
                        IE.IEDate = entity.IEDate;
                        IE.Reason = entity.Reason;
                        IE.Amount = entity.Amount;
                        ctx.Entry(IE).State = System.Data.Entity.EntityState.Modified;
                        if (ctx.SaveChanges() > 0)
                        {
                            entity = IE;
                            SystemLogManager.Insert("Cập nhật chi tiêu: " + entity);
                            return "Cập nhật thành công";
                        }
                        return "Cập nhật thất bại";
                    }
                    else
                        return "Mã chi tiết không tồn tại";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }
        }

        public string Delete(IE t)
        {
            IE entity = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                entity = ctx.IEs.Where(a => a.IEID == t.IEID).FirstOrDefault<IE>();
                if (entity != null)
                {
                    entity.Status = "Đã xóa";
                    ctx.Entry(entity).State = System.Data.Entity.EntityState.Modified;
                    if (ctx.SaveChanges()>0)
                    {
                        SystemLogManager.Insert("Xóa chi tiêu: " + entity);
                        return "Xóa thành công";
                    }
                    else
                        return "Xóa thất vại";
                }
                else
                    return "Mã chi tiêu không tồn tại";
            }
        }

        public DataTable SearchByReason(string reason)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                DataTable dt = CreateTable();
                var IEList = (from a in ctx.IEs where a.Reason.Contains(reason) select a).ToList<IE>();
                foreach (var row in IEList)
                {
                    dt.LoadDataRow(CreateRow(row), false);
                }
                return dt;
            }
        }

        public DataTable SearchByID(int id)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                DataTable dt = CreateTable();
                var IEList = (from a in ctx.IEs where a.IEID == id select a).ToList<IE>();
                foreach (var row in IEList)
                {
                    dt.LoadDataRow(CreateRow(row), false);
                }
                return dt;
            }
        }

        private DataTable CreateTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("IEID", typeof(string));
            dt.Columns.Add("EmployeeID", typeof(string));
            dt.Columns.Add("IEDate", typeof(DateTime));
            dt.Columns.Add("Reason", typeof(string));
            dt.Columns.Add("Amount", typeof(long));
            return dt;
        }

        public DataTable GetAll()
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                DataTable dt = CreateTable();
                var IEList = (from a in ctx.IEs where a.Status != "Đã xóa" select a).ToList<IE>();
                foreach (var row in IEList)
                {
                    dt.LoadDataRow(CreateRow(row), false);
                }
                return dt;
            }
        }

        public IE GetSignle(int id)
        {
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                ctx.Configuration.LazyLoadingEnabled = false;
                IE entity = (from a in ctx.IEs
                                  where a.IEID == id
                             select a).FirstOrDefault<IE>();
                ctx.Entry(entity).Reference(s => s.Employee).Load();
                return entity;
            }
        }

        public object[] CreateRow(IE entity)
        {
            return new object[] { entity.IEID, entity.EmployeeID, entity.IEDate, entity.Reason, entity.Amount };
        }
    }
}
